const getDialogTemplate = (componenetName) => `
<template>
  <commonDialog
    :visible="visible"
    title="产品批次"
    :baseConfig="{
      dialogWidth: '1100px',
      showSelection: true,
    }"
    :queryItems="dialog.queryItems"
    :baseForm="dialog.baseForm"
    :append-to-body="true"
    :tableData="dialog.dialogList"
    :current-page.sync="dialog.tablePage.pageNo"
    :page-size="dialog.tablePage.pageSize"
    :total="dialog.tablePage.totalRecord"
    :selectedList="dialog.selectedList"
    :calcSelectionFn="calcSelectionFn"
    @selectionChange="handleSelectChange"
    @currentPageChange="handleChange"
    @querySearch="queryDialog"
    @rowClick="handleDialog"
    @rowDbClick="dbClick"
    @confirm="saveDialog"
    @cancel="closeDialog"
    :columns="dialog.headerList"
  ></commonDialog>
</template>
<script>
import { productBatchListReturn } from '@/model/api/main/extend/performanceManage';
export default {
  name: '${componenetName}',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    calcSelectionFn: {
      type: Function,
      default: () => {},
    },
    ortherParams: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      dialog: {
        baseForm: {
          batchNo: '',
        },
        tablePage: {
          pageNo: 1,
          pageSize: 10,
          totalRecord: 0,
        },
        dialogList: [],
        headerList: [
          {
            name: '产品批次',
            code: 'productCode',
            width: '200',
          },
          {
            name: '产品型号',
            code: 'productDesc',
            width: '100',
          },
          {
            name: '产品名称',
            code: 'productName',
          },
          {
            name: '项目号',
            code: 'itemNo',
          },
          {
            name: '工单编号',
            code: 'woCode',
          },
        ],
        // 弹窗查询参数
        selection: {},
        selectedList: [],
        queryItems: [
          {
            label: '产品批次',
            name: 'productBatch',
          },
          {
            label: '产品名称',
            name: 'productName',
          },
          {
            label: '产品型号',
            name: 'productDesc',
          },
          {
            label: '产品编码',
            name: 'productCode',
          },
          {
            label: '项目号',
            name: 'itemNo',
          },
        ],
      },
    };
  },
  watch: {
    visible: {
      handler(v) {
        if (v) {
          this.handleGetData();
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 打开弹窗
    handleGetData(page) {
      let params = {
        ...this.dialog.baseForm,
        pageNo: page || this.dialog.tablePage.pageNo,
        pageSize: this.dialog.tablePage.pageSize,
        ...this.ortherParams,
      };
      this.params = params;
      productBatchListReturn(params).then(res => {
        if (!res.success) return;
        this.dialog.dialogList = res.rows || [];
        this.dialog.tablePage.totalRecord = res.count || 0;
      });
    },
    // 弹窗查询按钮
    queryDialog() {
      this.dialog.selection = {};
      this.dialog.tablePage.pageNo = 1;
      this.handleGetData();
    },
    // 行选择事件
    handleDialog(val) {
      this.dialog.selection = val;
    },
    // 双击
    dbClick(val) {
      const selectable = this.calcSelectionFn(val);
      if (selectable) {
        this.dialog.selectedList = [];
        this.dialog.selectedList.push(this.dialog.selection);
      }
      this.saveDialog();
      this.closeDialog();
    },
    // 多选框选择
    handleSelectChange(val) {
      this.dialog.selectedList = val;
    },
    // 分页
    handleChange(page) {
      this.dialog.tablePage.pageNo = page;
      this.dialog.selection = {};
      this.dialog.selectedList = [];
      this.handleGetData(page);
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('update:visible', false);
      // 清空并初始化弹窗
      this.dialog.baseForm = {
        batchNo: '',
      };
      this.dialog.selection = {};
      this.dialog.selectedList = [];
      this.dialog.tablePage.pageNo = 1;
    },
    // 保存弹窗
    saveDialog() {
      this.$emit('confirm', this.dialog.selectedList);
      this.closeDialog();
    },
  },
};
</script>
<style lang="scss" scoped></style>
`;

const getModifyTemplate = (componenetName) => [
  //template
  {
    repeatReg: new RegExp(`<${componenetName}`),
    targetReg: /(<\/\S*>[\s]*<\/template>[\s]*?<script>)/,
    tagetReplace: `<${componenetName} :visible.sync="dialogShow.${componenetName}" :calcSelectionFn="calcSelectionFn" @confirm="saveDialog('${componenetName}', $event)" />\n$1`,
  },
  // import
  {
    repeatReg: new RegExp(
      `import\\s${componenetName}\\sfrom\\s['"']\\.\\/${componenetName}\\.vue['"']`
    ),
    targetReg: /<script>([\s\S]*?)export/,
    tagetReplace: `<script>$1import ${componenetName} from './${componenetName}.vue';

export`,
  },
  // components
  {
    repeatReg: new RegExp(`components:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /components:\s*{/,
    addOptionReg: /(name:.*),/,
    addOptionReplace: `$1,
    components: {
      ${componenetName},
    },`,
    targetReg: /components:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(componenetName);
      return `components:{${keys.join(",")}},`;
    },
  },
  {
    // dialogShow
    repeatReg: new RegExp(`dialogShow:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /dialogShow:\s*{/,
    addOptionReg: /data\(\)\s*{([\s]*?)return\s*{/,
    addOptionReplace: `data\(\) {$1return {
    dialogShow: {
      ${componenetName}: false,
    },`,
    targetReg: /dialogShow:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(`${componenetName}:false`);
      return `dialogShow:{${keys.join(",")}},`;
    },
  },
  {
    // saveDialog
    repeatReg: null,
    checkOptionReg: /saveDialog\(\s*type\s*,\s*data\s*\)/,
    addOptionReg: /methods:\s*{/,
    addOptionReplace: `methods: {
      saveDialog(type, data) {
        switch (type) {
          case '${componenetName}': {
          }
          break;
        }
      },
      showDialog(type) {
        this.dialogShow[type] = true;
      },
      handleClear(type) {
        switch (type) {
          case '${componenetName}': {
            break;
          }
        }
      },`,
    targetReg: /saveDialog\(type, data\) \{\s*switch \(type\) \{/,
    tagetReplace: `saveDialog(type, data) {
      switch (type) {
        case '${componenetName}': {
        }
        break;`,
  },
  {
    // calcSelectionFn
    repeatReg: new RegExp(`calcSelectionFn\\(`),
    targetReg: /methods:\s*{/,
    tagetReplace: `methods: {
      calcSelectionFn(row) {
      let index = this.tableData.findIndex(item => item.produceBatch === row.produceBatch);
      if (index >= 0) {
        return false;
      }
      return true;
    },`,
  },
];
module.exports = {
  getDialogTemplate,
  getModifyTemplate,
};
